package com.hbue.dao.impl;

import com.hbue.bean.User;
import com.hbue.dao.MemberDao;
import com.hbue.util.DatabaseUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class MemberDaoImpl implements MemberDao {

    @Override
    public boolean findById(String username, String password) {
        //连接数据库
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtil.getConnection();
            statement = connection.prepareStatement("select * from admin where username=? and password =?");
            statement.setString(1, username);
            statement.setString(2, password);
            ResultSet resultSet = statement.executeQuery();
            if (resultSet.next()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public ArrayList<User> findAll() {
        //连接数据库
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtil.getConnection();
            statement = connection.prepareStatement("select * from user ");
            ResultSet rs = statement.executeQuery();
            ArrayList<User> users = new ArrayList<>();
            while (rs.next()) {
                int id = rs.getInt(1);
                String username = rs.getString(2);
                String password = rs.getString(3);
                String name = rs.getString(4);
                String sex = rs.getString(5);
                int age = rs.getInt(6);
                String phone = rs.getString(7);
                int userSign = rs.getInt(8);
                User user = new User(id, username, password, name, sex, age, phone, userSign);
                users.add(user);
            }
            return users;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void deleteById(String id) {
        //连接数据库
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtil.getConnection();
            statement = connection.prepareStatement("delete from user where userId = ?");
            statement.setString(1, id);
            statement.executeUpdate();
            //connection.commit();//提交
        } catch (Exception e) {
            e.printStackTrace();
            try {
                //connection.rollback();
            } catch (Exception throwables) {
            }
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 根据id找到用户信息
     *
     * @param id
     * @return
     */
    @Override
    public User findUserById(String id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtil.getConnection();
            statement = connection.prepareStatement("select * from user where userId = ? ");
            statement.setString(1, id);
            ResultSet rs = statement.executeQuery();
            User user = new User();
            while (rs.next()) {
                int userId = rs.getInt(1);
                String username = rs.getString(2);
                String password = rs.getString(3);
                String name = rs.getString(4);
                String sex = rs.getString(5);
                int age = rs.getInt(6);
                String phone = rs.getString(7);
                int userSign = rs.getInt(8);
                user = new User(userId, username, password, name, sex, age, phone, userSign);
            }
            return user;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 更新用户数据
     * @param user
     */
    @Override
    public void update(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtil.getConnection();
            statement = connection.prepareStatement("update user set  password = ?,username = ?, name= ? , age= ? ,sex= ? ,phone= ?  where username= ?");
            statement.setString(1,user.getUsername());
            statement.setString(2,user.getPassword());
            statement.setString(3,user.getName());
            statement.setInt(4,user.getAge());
            statement.setString(5,user.getSex());
            statement.setString(6,user.getPhone());
            statement.setString(7,user.getUsername());
            statement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


    @Override
    public void addUser(User user) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
           connection = DatabaseUtil.getConnection();
           statement=  connection.prepareStatement("insert into user values(null ,?,?,?,?,?,?,2)");
           statement.setString(1,user.getUsername());
           statement.setString(2,user.getPassword());
           statement.setString(3,user.getName());
           statement.setString(4,user.getSex());
           statement.setInt(5,user.getAge());
           statement.setString(6,user.getPhone());
           statement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    @Override
    public  ArrayList<User> findUserByIdAndUsername(String searchContent) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
          connection=  DatabaseUtil.getConnection();
          statement = connection.prepareStatement("select * from user where userId = ? or username = ? ");
          statement.setString(1,searchContent);
          statement.setString(2,searchContent);
            ResultSet resultSet = statement.executeQuery();
            ArrayList<User> users = new ArrayList<>();
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String username = resultSet.getString(2);
                String password = resultSet.getString(3);
                String name = resultSet.getString(4);
                String sex = resultSet.getString(5);
                int age = resultSet.getInt(6);
                String phone = resultSet.getString(7);
                User user = new User(id, username, password, name, sex, age, phone, 2);
                users.add(user);
            }
            return users;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 普通用户登录
     * @param username
     * @param password
     */
    @Override
    public User findUser(String username, String password) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = DatabaseUtil.getConnection();
            statement = connection.prepareStatement("select * from user where username=? and password=?");
            statement.setString(1,username);
            statement.setString(2,password);
            ResultSet resultSet = statement.executeQuery();
            User user = null;
            while (resultSet.next()){
                int id = resultSet.getInt(1);
                String _username = resultSet.getString(2);
                String _password = resultSet.getString(3);
                String name = resultSet.getString(4);
                String sex = resultSet.getString(5);
                int age = resultSet.getInt(6);
                String phone = resultSet.getString(7);
                user = new User(id, _username, _password, name, sex, age, phone, 2);
            }
            return user;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


}
